package sentinel;


//漏桶算法 简易实现
public class Class03_LeakyBucket_01 {

    public long timeStemp = System.currentTimeMillis();//当前时间
    public int capital = 100;//桶容量
    public int rate =10 ;//水漏出的速度(每秒系统能处理的请求数)
    public int water = 0;//当前累积的请求

    public boolean limit(){
        long now = System.currentTimeMillis();
        water = Math.max(0, (water - (int) ((now - timeStemp) / 1000) * rate ) ); // 先执行漏水,计算剩余水量
        timeStemp = now;
        if(water + 1 > capital){
            return false;
        }else{
            water++;
            return true;
        }

    }

}
